package com.mililoan.console.controller.system;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mililoan.common.bean.CommonResult;
import com.mililoan.common.bean.JqgridRequest;
import com.mililoan.common.bean.JqgridResultSet;
import com.mililoan.common.bean.JqgridResultSetFactory;
import com.mililoan.common.bean.PageResultSet;
import com.mililoan.model.system.Actor;
import com.mililoan.model.system.Function;
import com.mililoan.service.module.system.ActorBo;
import com.mililoan.service.module.system.FuncBo;

@Controller
@RequestMapping("/actor")
public class ActorAction {
	private static Logger LOGGER = LoggerFactory.getLogger(ActorAction.class);
	private static final String	MOUDLE	= "sysmrg";	
	@Autowired
	private ActorBo actorBo;
	@Autowired
	private FuncBo funcBo;
	
	/**
	 * @Title: toActorList
	 * @Description: 跳转到查询角色信息页面
	 * @author 
	 * @return
	 */
	@RequestMapping("/toActorList.do")
	public String toActorList(Model model) {
		LOGGER.info("跳转到查询角色信息页面");
		return MOUDLE + "/actorList";
	}
	
	/**
	 * @Title: showUpdate
	 * @Description: 打开修改页面
	 * @author
	 * @return
	 */
	@RequestMapping("/toUpdateActor.do")
	public String toUpdateUser(@RequestParam(value="actorId", required = false) String actorId, Model model){
		LOGGER.info("跳转到角色信息新建或编辑页面");
		Actor actor = null;
		//查询模块
		ArrayList<Function> modules = funcBo.getAllModules(); 
		//查询所有的功能
		ArrayList<Function> funcs = funcBo.getAllFuncs();
		
		if(null != actorId){
			actor = actorBo.getActorById(Integer.parseInt(actorId));
		} else {
			actor = new Actor();
		}
		model.addAttribute("actor", actor);
		model.addAttribute("modules", modules);
		model.addAttribute("funcs", funcs);
		return MOUDLE + "/actorUpdate";
	}
	
	/**
	 * @Title: queryAjax
	 * @Description: 查询角色列表信息
	 * @author
	 * @return
	 */
	@RequestMapping("queryActorList.do")
	@ResponseBody
	public JqgridResultSet<Actor> queryActorList(@RequestBody JqgridRequest<Actor> jqgridRequest) {
		Actor actor = jqgridRequest.getObj();
		if (null == actor) {
			actor = new Actor();
		}
		//分页查询
		PageResultSet<Actor> prs = actorBo.queryActor(actor, jqgridRequest.getPage(), jqgridRequest.getRows());
		return JqgridResultSetFactory.toJqgridResultSet(prs);
	}
	
	@RequestMapping(value = "/updateActor.do", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updateActor(@RequestBody Actor actor) throws Exception {
		CommonResult commonResult = null;
		Actor actorObj = null;
		//前台传字符串
		if(null != actor.getFunCodeArrStr()){
			String[] funCodeArray = actor.getFunCodeArrStr().split(",");
			ArrayList<Integer> funCodes = new ArrayList<Integer>();
			for (int i = 0; i < funCodeArray.length; i++) {
				funCodes.add(Integer.parseInt(funCodeArray[i]));
				//父模块添加
				if(!funCodes.contains(Integer.parseInt(funCodeArray[i].substring(0, 1)+"00"))){
					funCodes.add(Integer.parseInt(funCodeArray[i].substring(0, 1)+"00"));
				}
			}
			actor.setFunCodes(funCodes);
		}
		
		if(null == actor.getId()|| 0 == actor.getId()){
			actorObj = new Actor();
			//前台取数
			actorObj.setActorName(actor.getActorName());
			actorObj.setSummary(actor.getSummary());
			actorObj.setFunCodes(actor.getFunCodes());
			actorObj.setDepCode(actor.getDepCode());
			actorObj.setisvalid("1");
			
			if (actorBo.addActor(actorObj)) {
				commonResult = new CommonResult();
				commonResult.setResultCode(CommonResult.SUCCESS);
				commonResult.setMessage("新增角色成功!");
			} else {
				commonResult = new CommonResult();
				commonResult.setResultCode(CommonResult.FAIL);
				commonResult.setMessage("新增角色失败!");
			}
		} else {
			actorObj = actorBo.getActorById(actor.getId());
			//前台取数
			actorObj.setActorName(actor.getActorName());
			actorObj.setSummary(actor.getSummary());
			actorObj.setFunCodes(actor.getFunCodes());
			actorObj.setDepCode(actor.getDepCode());
			actorObj.setisvalid("1");
			// 修改权限
			if (actorBo.updateActor(actorObj)) {
				commonResult = new CommonResult();
				commonResult.setResultCode(CommonResult.SUCCESS);
				commonResult.setMessage("修改角色权限成功!");
			} else {
				commonResult = new CommonResult();
				commonResult.setResultCode(CommonResult.FAIL);
				commonResult.setMessage("修改角色权限失败!");
			}
		}
		return commonResult;
	}
	
	@RequestMapping(value = "/delActor.do", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult delActor(@RequestBody Actor actor){
		CommonResult commonResult = null;
		return null;
	}
}
